<?php

namespace gogrpc;

use Proto\TencentImServiceClient;

class TencentTimServer extends BaseServer
{

    protected static $client   = null;
    protected static $metadata = [];

    public function __construct()
    {
        // 日志文件
        parent::$log_file = 'tencent_tim_grpc.log';

        self::$metadata = [
            "app_id"     => [config('grpc.app_id')],
            "app_secret" => [config('grpc.app_secret')],
        ];

        if (!self::$client) {
            self::$client = new TencentImServiceClient(config('grpc.tim_sever'), [
                'credentials' => \Grpc\ChannelCredentials::createInsecure(),
            ]);
        }
    }

    /**
     * 主动接入接入私聊
     * @param $admin_id
     * @param $anchor_id
     * @param $user_id
     * @param int $session_id
     * @return bool
     */
    public function startChatSession($admin_id, $anchor_id, $user_id, $session_id = 0)
    {
        $request = new \Proto\StartChatSessionReq();
        $request->setAdminId($admin_id);
        $request->setAnchorId($anchor_id);
        $request->setUserId($user_id);
        $request->setSessionId($session_id);

        list($response, $status) = self::$client->StartChatSession($request, self::$metadata)->wait();
        if ($status->code == \Grpc\STATUS_OK) {
            return true;
        }
        // 记录日志
        parent::writeLog(__FUNCTION__, $status);
        return false;
    }

    /**
     * 待办接入
     * @param $uid
     * @return bool
     */
    public function wiringChatSession($admin_id, $session_id)
    {
        $request = new \Proto\WiringChatSessionReq();
        $request->setAdminId($admin_id);
        $request->setChatSessionId($session_id);

        list($response, $status) = self::$client->wiringChatSession($request, self::$metadata)->wait();
        if ($status->code == \Grpc\STATUS_OK) {
            return true;
        }
        // 记录日志
        parent::writeLog(__FUNCTION__, $status);
        return false;
    }

    /**
     * 转入待办
     * @param $uid
     * @return bool
     */
    public function transChatSession($session_id)
    {
        $request = new \Proto\TransChatSessionReq();
        $request->setChatSessionId($session_id);

        list($response, $status) = self::$client->TransChatSession($request, self::$metadata)->wait();
        if ($status->code == \Grpc\STATUS_OK) {
            return true;
        }
        // 记录日志
        parent::writeLog(__FUNCTION__, $status);
        return false;
    }

    /**
     * 结束私聊
     * @param $uid
     * @return bool
     */
    public function endChatSession($session_id)
    {
        $request = new \Proto\EndChatSessionReq();
        $request->setChatSessionId($session_id);

        list($response, $status) = self::$client->EndChatSession($request, self::$metadata)->wait();
        if ($status->code == \Grpc\STATUS_OK) {
            return true;
        }
        // 记录日志
        parent::writeLog(__FUNCTION__, $status);
        return false;
    }


    /**
     * 发送私聊消息
     * @param $admin_id
     * @param $session_id
     * @param $group_id
     * @param $user_id
     * @param $msg
     * @return bool
     */
    public function customRelpy($admin_id, $session_id,$msg,$img_url)
    {


        $request = new \Proto\CustomReplyReq();
        $request->setAdminId($admin_id);
        $request->setContent($msg);
        $request->setSessionId($session_id);
        $request->setImgUrl($img_url);
//        // 消息内容为json字符串
//        $request->setContent(json_encode($content, JSON_UNESCAPED_UNICODE));

        list($response, $status) = self::$client->CustomReply($request, self::$metadata)->wait();
        if ($status->code == \Grpc\STATUS_OK) {
            return true;
        }
        // 记录日志
        parent::writeLog(__FUNCTION__, $status);
        return false;
    }

    /**
     * 发送群聊消息
     * @param $admin_id
     * @param $group_id
     * @param $msg
     * @param string $img
     * @return bool
     */
    public function sendGroupChatMsg($admin_id, $group_id, $msg, $img = '')
    {
        $request = new \Proto\SendGroupChatMsgReq();
        $request->setAdminId($admin_id);
        $request->setGroupChatId($group_id);
        $request->setMsgContent($msg);
        $request->setImageUrl($img);

        list($response, $status) = self::$client->SendGroupChatMsg($request, self::$metadata)->wait();
        if ($status->code == \Grpc\STATUS_OK) {
            return $response->getMsgId();
        }
        // 记录日志
        parent::writeLog(__FUNCTION__, $status);
        return false;
    }


    /**
     * 创建群聊
     * @param $admin_id 后台管理员id
     * @param $anchor_id 主播id
     * @param $name 名称
     * @param $avatar 头像
     * @param $introduce 群介绍
     * @param $desc 欢迎语
     * @return bool
     */
    public function createGroupChat($admin_id, $anchor_id, $name, $avatar, $introduce, $desc)
    {
        $request = new \Proto\CreateGroupChatReq();
        $request->setName($name);
        $request->setAvatar($avatar);
        $request->setIntroduce($introduce);
        $request->setWelcomeDesc($desc);
        $request->setAnchorId($anchor_id);
        $request->setCreateAdminId($admin_id);

        list($response, $status) = self::$client->CreateGroupChat($request, self::$metadata)->wait();
        if ($status->code == \Grpc\STATUS_OK) {
            return $response->getGroupChatId();
        }
        // 记录日志
        parent::writeLog(__FUNCTION__, $status);
        return false;
    }

    /**
     * @param $group_id 群组id
     * @param $user_id 用户id
     * @param $ban_time 禁言时间-分钟
     * @param $reason 禁言原因
     * @param $ban_type 禁言编号
     * @param $admin_id 管理员ID
     * @return bool
     */
    public function banUser($group_id, $user_id, $ban_time, $reason, $ban_type, $admin_id)
    {
        $request = new \Proto\BanUserReq();
        $request->setGroupChatId($group_id);
        $request->setUserId($user_id);
        $request->setDuration($ban_time);
        $request->setReason($reason);
        $request->setBanType($ban_type);
        $request->setAdminId($admin_id);

        list($response, $status) = self::$client->BanUser($request, self::$metadata)->wait();
        if ($status->code == \Grpc\STATUS_OK) {
            return true;
        }
        // 记录日志
        parent::writeLog(__FUNCTION__, $status);
        return false;
    }

    /**
     * 解散群聊
     * @param $group_id 群组id
     * @return bool
     */
    public function dissolveGroupChat($group_id)
    {
        $request = new \Proto\DissolveGroupChatReq();
        $request->setGroupChatId($group_id);

        list($response, $status) = self::$client->DissolveGroupChat($request, self::$metadata)->wait();
        if ($status->code == \Grpc\STATUS_OK) {
            return true;
        }
        // 记录日志
        parent::writeLog(__FUNCTION__, $status);
        return false;
    }

    /**
     * 群组-踢人
     * @param $group_id 群组id
     * @param $user_ids 用户id，一维数组
     * @return bool
     */
    public function kickUser($group_id, $user_ids)
    {
        if (!is_array($user_ids)) {
            $user_ids = [$user_ids];
        }

        $request = new \Proto\KickUserReq();
        $request->setGroupChatId($group_id);
        $request->setUserIds($user_ids);

        list($response, $status) = self::$client->KickUser($request, self::$metadata)->wait();
        if ($status->code == \Grpc\STATUS_OK) {
            return true;
        }
        // 记录日志
        parent::writeLog(__FUNCTION__, $status);
        return false;
    }


    /**
     * 群组-设置房管
     * @param $group_id 群组id
     * @param $user_id 用户id
     * @return bool
     */
    public function assignGroupChatAdmin($group_id, $user_id)
    {
        $request = new \Proto\AssignGroupChatAdminReq();
        $request->setGroupChatId($group_id);
        $request->setUserId($user_id);

        list($response, $status) = self::$client->AssignGroupChatAdmin($request, self::$metadata)->wait();
        if ($status->code == \Grpc\STATUS_OK) {
            return true;
        }
        // 记录日志
        parent::writeLog(__FUNCTION__, $status);
        return false;
    }

    /**
     * 群组-取消房管
     * @param $group_id 群组id
     * @param $user_id 用户id
     * @return bool
     */
    public function cancelAssignGroupChatAdmin($group_id, $user_id)
    {
        $request = new \Proto\CancelAssignGroupChatAdminReq();
        $request->setGroupChatId($group_id);
        $request->setUserId($user_id);

        list($response, $status) = self::$client->CancelAssignGroupChatAdmin($request, self::$metadata)->wait();
        if ($status->code == \Grpc\STATUS_OK) {
            return true;
        }
        // 记录日志
        parent::writeLog(__FUNCTION__, $status);
        return false;
    }

    /**
     * 群组-撤回消息
     * @param $group_id 群组id
     * @param $msg_id 消息id，seq字段
     * @return bool
     */
    public function revokeMessage($group_id, $msg_id)
    {
        $request = new \Proto\RevokeMessageReq();
        $request->setGroupId($group_id);
        $request->setMsgId($msg_id);

        list($response, $status) = self::$client->RevokeMessage($request, self::$metadata)->wait();
        if ($status->code == \Grpc\STATUS_OK) {
            return true;
        }
        // 记录日志
        parent::writeLog(__FUNCTION__, $status);
        return false;
    }

    /**
     * @param $group_id
     * @param $user_id
     * @return bool
     */
    public function cancelBanUser($group_id, $user_id, $admin_id)
    {
        $request = new \Proto\CancelBanUserReq();
        $request->setFansGroupId($group_id);
        $request->setBanUserId($user_id);
        $request->setOperateUserId($admin_id);

        list($response, $status) = self::$client->CancelBanUser($request, self::$metadata)->wait();
        if ($status->code == \Grpc\STATUS_OK) {
            return true;
        }
        // 记录日志
        parent::writeLog(__FUNCTION__, $status);
        return false;
    }

    /**
     * 删除消息
     * @param $group_id
     * @param $msg_id
     * @param $admin_id
     * @param $admin_name
     * @return bool
     */
    public function deleteMessage($group_id, $msg_id, $admin_id, $admin_name)
    {
        $request = new \Proto\DeleteMessageReq();
        $request->setGroupId($group_id);
        $request->setMsgId($msg_id);
        $request->setAdminId($admin_id);
        $request->setMgrId(0);
        $request->setAdminName($admin_name);

        list($response, $status) = self::$client->DeleteMessage($request, self::$metadata)->wait();
        if ($status->code == \Grpc\STATUS_OK) {
            return true;
        }
        // 记录日志
        parent::writeLog(__FUNCTION__, $status);
        return false;
    }

    /**
     * 删除房管私聊消息
     * @param $msg_id
     * @return bool
     */
    public function delMgrSingleMsg($msg_id)
    {
        $request = new \Proto\DelMgrSingleMsgReq();
        $request->setMsgId($msg_id);

        list($response, $status) = self::$client->DelMgrSingleMsg($request, self::$metadata)->wait();
        if ($status->code == \Grpc\STATUS_OK) {
            return true;
        }
        // 记录日志
        parent::writeLog(__FUNCTION__, $status);
        return false;
    }

    /**
     * 删除房管私聊消息
     * @param $msg_id
     * @return bool
     */
    public function BatchCustomReply($content, $users, $all)
    {
        $request = new \Proto\BatchCustomReplyReq();
        $request->setContent($content);
        $request->setUsers($users);
        $request->setAll($all);

        list($response, $status) = self::$client->BatchCustomReply($request, self::$metadata)->wait();
        if ($status->code == \Grpc\STATUS_OK) {
            return true;
        }
        // 记录日志
        parent::writeLog(__FUNCTION__, $status);
        return false;
    }

    // 开启活动
    public function ActivityBarrage($id, $toggle)
    {
        $request = new \Proto\ActivityReq();
        $request->setAnchorId($id);
        $request->setStatus($toggle);

        list($response, $status) = self::$client->ActivityBarrage($request, self::$metadata)->wait();
        if ($status->code == \Grpc\STATUS_OK) {
            return true;
        }
        // 记录日志
        parent::writeLog(__FUNCTION__, $status);
        return false;
    }

    // 开启活动
    public function CleanChatHis($id)
    {
        $request = new \Proto\CleanChatHisReq();
        $request->setRoomId($id);

        list($response, $status) = self::$client->CleanChatHis($request, self::$metadata)->wait();

        if ($status->code == \Grpc\STATUS_OK) {
            return true;
        }
        // 记录日志
        parent::writeLog(__FUNCTION__, $status);
        return false;
    }
}
